package com.derbysoft.lowcostadapter.commons.redis.service.impl;

import com.derbysoft.lowcostadapter.commons.model.RoomOccupancy;
import com.derbysoft.lowcostadapter.commons.redis.service.JedisClusterService;
import com.derbysoft.lowcostadapter.commons.redis.service.OccupancyCacheService;
import com.derbysoft.lowcostadapter.commons.redis.service.RedisSerializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by : jason
 * DateTime: 12-2-9
 */
@Component("occupancyCacheService")
public class OccupancyCacheServiceImpl implements OccupancyCacheService {

    @Autowired
    private JedisClusterService jedisClusterService;

    @Override
    public List<RoomOccupancy> loadAll(String key) {
        List<RoomOccupancy> roomOccupancies = new ArrayList<RoomOccupancy>();
        Map<String, RedisSerializable> roomOccupanciesMap = jedisClusterService.hGetAll(key);
        for (Map.Entry<String, RedisSerializable> entries : roomOccupanciesMap.entrySet()) {
            roomOccupancies.add((RoomOccupancy) entries.getValue());
        }
        return roomOccupancies;
    }

    @Override
    public void save(RoomOccupancy roomOccupancy) {
        jedisClusterService.hset(roomOccupancy);
    }

    @Override
    public void saveAll(List<RoomOccupancy> roomOccupancies) {
        jedisClusterService.pipelined(roomOccupancies);
    }
}
